#pragma once

#include "type.h"
#include "matrix4.h"
#include "AxisAlignedBox.h"
#include "vector3.h"

class TiXmlElement;
namespace ChunkSystem
{
	class IRenderer;
	class IRenderable;
	class IRenderableFactory;


	class ChunkItem
	{
	public:

		class LoadProgressListener
		{
		public:
			LoadProgressListener( ChunkItem* pTarget );
			virtual ~LoadProgressListener(void);
			
			void	SetProgressNotifyInterval( uint16 uInterval );
			uint16	GetProgressNotifyInterval( void );

			virtual void OnProgressIntervalNotify( uint16 uPercentage ) = 0;
		
		protected:
			uint16	m_uProgressInterval;
			ChunkItem*	m_pTarget;
		};
		typedef std::vector< LoadProgressListener* >	ConProgressListeners;

		ChunkItem( const String& strId, ChunkItem* pOwner );
		virtual ~ChunkItem(void);

		//Attach loading progress listener
		void AttachLoadProgressListener( LoadProgressListener* pListener );
		void DettachLoadProgressListener( LoadProgressListener* pListener );

		virtual bool Load( TiXmlElement* pChunkItemData ) = 0;
		virtual void Unload( void ) = 0;

		virtual bool InitSync( void );

		String GetId( void ) const{ return m_strId;}

		Common::Matrix4 GetWorldTM(void);
		void Transform( const Common::Matrix4& rTransform );

		const Common::AxisAlignedBox& GetAABB( void ) const { return m_aabb; }
		virtual void UpdateAABB( void ) = 0;

		ChunkItem* GetOwner(void) const{ return m_pOwner; }

	protected:

		void _OnLoad( uint16 uPercentage );

	protected:

		ChunkItem*		m_pOwner;

		String			m_strId;

		Common::Matrix4	m_matWorldTM;

		Common::AxisAlignedBox m_aabb;

		ConProgressListeners	m_conProgressListeners;
	};

	class ChunkItemFactory
	{
	public:
		ChunkItemFactory(){}
		virtual ~ChunkItemFactory(){}

		virtual String GetType(void) = 0;
		virtual ChunkItem* Create( ChunkItem* pOwner, TiXmlElement* pChunkItemData ) = 0;
	};

	typedef std::vector< ChunkItemFactory* >				ChunkItemFactories; 
	typedef ChunkItemFactories::iterator					ItrChunkItemFactories;
}